package com.spring.springbootmodel.controller;

import com.spring.springbootmodel.common.Result;
import com.spring.springbootmodel.entyti.User;
import com.spring.springbootmodel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：黄龙华
 * 功能：接收前端数据
 * 日期：2023/11/4 15:46
 */
@RestController
@RequestMapping("/user")
@CrossOrigin    //解决跨域访问问题
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 新增用户信息
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        try {
            userService.insertUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user) {
        try {
            userService.updatetUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除用户信息（单）
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteUserOneById(@PathVariable Integer id) {
        try {
            userService.deleteUserOneById(id);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除用户信息（多）
     */
    @DeleteMapping("/delete/batch")
    public Result deleteUsersOneByIds(@RequestBody List<Integer> ids) {
        try {
            userService.deleteUsersOneByIds(ids);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 查询全部用户信息
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> users = userService.selectAll();
        return Result.success(users);
    }

    /**
     * 根据id查询用户信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }

    /**
     * 根据name查询多条用户信息
     */
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name) {
        List<User> users = userService.selectByName(name);
        return Result.success(users);
    }

    /**
     * 多条件查询
     * 根据name和username查询多条用户信息
     */
    @GetMapping("/select/more")
    public Result selectByNameAndUsername(@RequestParam String name, @RequestParam String username) {
        List<User> users = userService.selectByNameAndUsername(name, username);
        return Result.success(users);
    }

    /**
     * 多条件查询(模糊查询)
     * 根据name和username查询多条用户信息
     */
    @GetMapping("/select/mo")
    public Result selectByMo(@RequestParam String name, @RequestParam String username) {
        List<User> users = userService.selectByMo(name, username);
        return Result.success(users);
    }

    /**
     * 多条件查询(分页模糊查询)
     * 根据name和username查询多条用户信息
     */
    @GetMapping("/select/limit")
    public Result selectByLimit(@RequestParam String name,
                                @RequestParam String username,
                                @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        Map<String, Object> map = userService.selectByLimit(name, username,pageNum,pageSize);
        return Result.success(map);
    }

}